<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class GoodsController extends CommonController{
	private $goodsModel;
	private $gdModel;
	private $gattrModel;
	private $cateModel;
	private $brandModel;
	private $attrModel;
	//构造函数
	public function __init()
	{
		parent::__init();
		$this->goodsModel = new \Common\Model\Goods;
		$this->gdModel = new \Common\Model\Gdetail;
		$this->gattrModel = new \Common\Model\Gattr;
		$this->cateModel = new \Common\Model\Category;
		$this->brandModel = new \Common\Model\Brand;
		$this->attrModel = new \Common\Model\Attr;
	}
	
//	商品列表
	public function index(){
		$total = $this->goodsModel->count();
		$page = Page::row(10)->desc(array('pre'=>'<<上一页', 'next'=>'下一页>>'))->make($total);
		$goodsData = $this->goodsModel->limit(Page::limit())->get();
		View::with('page',$page);
		View::with('goodsData',$goodsData);
		View::make();
	}
	
//	添加商品
	public function add(){
//		添加
		if(IS_POST){
			if(!$this->goodsModel->store()) View::error($this->goodsModel->getError());
			View::success('添加成功',U('index'));
		}
//		初始化数据
//		1.分类表
		$cateData = $this->cateModel->get();
		$cateData = Data::tree($cateData,'cname','cid','pid');
		View::with('cateData',$cateData);
//		2.所属品牌
		$brandData = $this->brandModel->get();
		View::with('brandData',$brandData);
		
		View::make();
	}
	
//	编辑商品
	public function edit(){
//		获得编辑商品Id
		$gid = Q('get.gid',0,'intval');
		
		if(IS_POST){
			if(!$this->goodsModel->edit()) View::error($this->goodsModel->getError());
			View::success('修改成功！',U('index'));
		}
//		获得类型id
		$tid = $this->goodsModel->where("gid = {$gid}")->pluck("type_tid");
		
//		1.基本商品信息调出
		$goodsData = $this->goodsModel->where("gid = {$gid}")->find();
		View::with('goodsData',$goodsData);
//		商品详细内容表
		$gdData = $this->gdModel->where("goods_gid = {$gid}")->find();
//		图片地址由字符串形式转为数组形式
		$gdData['medium'] = explode(',', $gdData['medium']);
		
		View::with('gdData',$gdData);
//		商品分类表
		$cateData = $this->cateModel->get();
		$cateData = Data::tree($cateData,'cname','cid','pid');
		View::with('cateData',$cateData);
//		商品品牌表
		$brandData = $this->brandModel->get();
		View::with('brandData',$brandData);
		
//		商品属性表
		$attr = $this->attrModel->where("class=0 AND type_tid = {$tid}")->get();
//		将atvalue字符串转换为数组
		foreach ($attr as $k => $v) {
			$attr[$k]['atvalue'] = explode(',', $v['atvalue']);	
		}
		View::with('attr',$attr);
		
		
		
//		商品规格
//		p($attr);
		$spec = $this->attrModel->where("class=1 AND type_tid = {$tid}")->get();
//		将atvalue字符串转换为数组
		foreach ($spec as $k => $v) {
			$spec[$k]['atvalue'] = explode(',', $v['atvalue']);
		}
		View::with('spec',$spec);
		
		
//		列表图片
//		图片地址

		
		
		
		
		$gattrData = $this->gattrModel->where("goods_gid = {$gid}")->get();
		View::with('gattrData',$gattrData);
		View::make();
	}



	
//	商品删除
	public function del(){
//		获得要删除的id
//		1.商品表的id
		$gid = Q('get.gid',0,'intval');
//		商品列表图的地址
		$listpic = $this->goodsModel->where("gid = {$gid}")->pluck('listpic');
//		大中小图地址
		$mini = $this->gdModel->where("goods_gid = {$gid}")->pluck('mini');
		$mini = explode(',', $mini);
		$medium = $this->gdModel->where("goods_gid = {$gid}")->pluck('medium');
		$medium = explode(',', $medium);
		$big = $this->gdModel->where("goods_gid = {$gid}")->pluck('big');
		$big = explode(',', $big);
//		删除商品详细表、
		$this->gdModel->where("goods_gid = {$gid}")->delete();
		if($mini){
			foreach ($mini as $v) {
				if(is_file($v))
				unlink($v);
			}
		};
		if($medium){
			foreach ($medium as $v) {
				if(is_file($v))
				unlink($v);
			}
		}
		if($big){
			foreach ($big as $v) {
				if(is_file($v))
				unlink($v);
			}
		}
//		删除商品属性表
		$this->gattrModel->where("goods_gid = {$gid}")->delete();
//		删除货品表
		$glModel = new \Common\Model\Glist;
		$glModel->where("goods_gid = {$gid}")->delete();
//		删除主商品表
		$this->goodsModel->where("gid = {$gid}")->delete();
		if(is_file($listpic)) unlink($listpic);
		
		View::success('删除成功',U('index'));
		
	}
	
	
	public function attr(){
		$tid = Q('post.tid',0,'intval');
		$attrData = $this->attrModel->where("type_tid = {$tid}")->get();
		foreach ($attrData as $k => $v) {
			$attrData[$k]['atvalue'] = explode(',', $v['atvalue']);
		}
		if($attrData){
			echo json_encode($attrData);
		}else{
			echo 0;
		}
	}
	
//	列表图单图上传
	public function listpic(){
	    $file = Upload::path('Upload/listpic/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
	                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
	                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
	                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	
	
	
//	多商品图片上传
	public function upload(){
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
	                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
	                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
	                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	
//	异步删除图片
	public function ajaxdel(){
		$path = Q('post.path');
		$gid = Q('post.gid',0,'intval');
		
		if(is_file($path)){
			$type = strrchr($path, '.');
			$typelen = strlen($type);
			$oldPath = substr($path, -($typelen+7),-$typelen);
//			如果是原来的图的处理方式
			if($oldPath !='_medium' && $oldPath !='listpic'){
			unlink($path);
			}
			
			echo json_encode(array('status'=>true));
		}else{
			echo json_encode(array('status'=>false));
		}
	}
	


}






